package io.renren.utils.util;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class CloseUtils
{
	private CloseUtils() {
        
    }

    /**
     * 关闭实现了closeable接口的对象，比如InputStream，OutputStream
     * @param closeable
     */
    public static void close(Closeable... closeable){
    	if (closeable != null && closeable.length > 0) {
            for (Closeable c : closeable) {
                if (c != null) {
                    try {
                        c.close();
                    } catch (IOException e) { 
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    
    /**
     * 关闭输入流
     * @param iss
     */
    public static void closeInputStream(InputStream... iss) {
        if (iss != null && iss.length > 0) {
            for (InputStream is : iss) {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    /**
     * 关闭输出流
     * @param oss
     */
    public static void closeOutputStream(OutputStream... oss) {
        if (oss != null && oss.length > 0) {
            for (OutputStream os : oss) {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    /**
     * 关闭字符输入流
     * @param readers
     */
    public static void closeReader(Reader... readers) {
        if (readers != null && readers.length > 0) {
            for (Reader reader : readers) {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    /**
     * 关闭字符输出流
     * @param writers
     */
    public static void closeWriter(Writer... writers) {
        if (writers != null && writers.length > 0) {
            for (Writer writer : writers) {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
